package com.ctshk.rpc.order.train.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.OrderCollectionStatus;
import com.ctshk.common.enums.SystemBusinessType;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.exception.BusinessException;
import com.ctshk.rpc.finance.req.bill.BillOperateRefundReq;
import com.ctshk.rpc.finance.service.IFinanceBillService;
import com.ctshk.rpc.order.train.entity.TrainOrderCollection;
import com.ctshk.rpc.order.train.mapper.TrainOrderCollectionMapper;
import com.ctshk.rpc.order.train.service.ITrainOrderCollectionService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * <p>
 * 火车收款明细 服务实现类
 * </p>
 *
 * @author 彭柳霖
 * @since 2021-03-17
 */
@Slf4j
@DubboService
public class TrainOrderCollectionServiceImpl extends ServiceImpl<TrainOrderCollectionMapper, TrainOrderCollection> implements ITrainOrderCollectionService {

    @Autowired
    private TrainOrderCollectionMapper trainOrderCollectionMapper;

    @DubboReference
    private IFinanceBillService financeBillService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result reconfirm(Long id, Long userId) {
        TrainOrderCollection trainOrderCollection = trainOrderCollectionMapper.selectById(id);
        if (trainOrderCollection == null) {
            //收款记录不存在
            throw new BusinessException(SystemError.TRAIN_PRODUCT_14026);
        }
        if (trainOrderCollection.getStatus() == null || trainOrderCollection.getStatus() != OrderCollectionStatus.REJECT.getCode()) {
            // 收款记录的状态必须是驳回，才能再次确认
            throw new BusinessException(SystemError.TRAIN_PRODUCT_14027);
        }
        trainOrderCollection.setStatus(OrderCollectionStatus.UN_REJECT.getCode());
        trainOrderCollection.setRejectInfo("");
        trainOrderCollection.setModifiedId(userId);
        trainOrderCollection.setGmtModified(LocalDateTime.now());
        trainOrderCollectionMapper.updateById(trainOrderCollection);
        /**
         * 二次确认，同步状态到财务
         */
        BillOperateRefundReq billOperateRefundReq = new BillOperateRefundReq();
        billOperateRefundReq.setBillNumber(trainOrderCollection.getCollectionNumber());
        billOperateRefundReq.setBusinessType(SystemBusinessType.TRAIN_TICKET.getCode());
        billOperateRefundReq.setUserId(userId);
        log.info("【火车票订单收款】二次确认财务收款记录，req:{}", JSON.toJSONString(billOperateRefundReq));
        financeBillService.reConfirm(billOperateRefundReq);
        return Result.success();
    }

    @Override
    public Result rejectPayment(String collectionNumber, Long userId, String msg) {
        TrainOrderCollection trainOrderCollection = baseMapper.selectOne(Wrappers.<TrainOrderCollection>lambdaQuery()
                .eq(TrainOrderCollection::getCollectionNumber, collectionNumber));
        if (trainOrderCollection == null) {
            //收款记录不存在
            return Result.failed(SystemError.AIREXPRESS_ORDER_60014);
        }
        if (!Objects.equals(trainOrderCollection.getStatus(), OrderCollectionStatus.REJECT.getCode())) {
            // 收款记录的状态必须不等于“已驳回”
            return Result.failed(SystemError.AIREXPRESS_ORDER_60015);
        }
        trainOrderCollection.setStatus(OrderCollectionStatus.REJECT.getCode());
        trainOrderCollection.setRejectInfo(msg);
        trainOrderCollection.setModifiedId(userId);
        trainOrderCollection.setGmtModified(LocalDateTime.now());
        baseMapper.updateById(trainOrderCollection);
        return Result.success();
    }


}
